package ee.helmesWorkTest.backend.database;

import java.util.ResourceBundle;

import ee.helmesWorkTest.GeneralConstants;
import ee.helmesWorkTest.backend.database.connectionproperties.DatabaseConnectionProperties;
import ee.helmesWorkTest.backend.database.connectionproperties.DatabaseConnectionPropertiesFactory;
import ee.helmesWorkTest.utils.PropertiesFileLoader;

public class ConnectionManagerInitializer 
{
	private static final String PROP_NAME_FOR_DATABASE_TYPE = "db_type";
	
	public ConnectionManagerInitializer()
	{
	
	}
	
	public static void initializeConnectionManagerDBConnectionProperties()
	{
		DatabaseConnectionProperties databaseConnectionProperties = 
				DatabaseConnectionPropertiesFactory.getDatabaseConnectionProperties(getDatabaseTypeFromDatabaseConnectionPropertiesFile());
		
		ConnectionManager connectionManagerInstance = ConnectionManager.getInstance();
		connectionManagerInstance.setDatabaseType(databaseConnectionProperties.getDatabaseType());
		connectionManagerInstance.setHost(databaseConnectionProperties.getHost());
		connectionManagerInstance.setPort(databaseConnectionProperties.getPort());
		connectionManagerInstance.setDatabaseName(databaseConnectionProperties.getDatabaseName());
		connectionManagerInstance.setUsername(databaseConnectionProperties.getUserName());
		connectionManagerInstance.setPassword(databaseConnectionProperties.getPassword());
		
		connectionManagerInstance.setDatabaseConnectionString(databaseConnectionProperties.getDatabaseConnectionString());
	}
	
	private static DBType getDatabaseTypeFromDatabaseConnectionPropertiesFile()
	{
		PropertiesFileLoader propertiesFileLoader = new PropertiesFileLoader(GeneralConstants.CONNECTION_PROPERTIES_PATH);
		ResourceBundle properties = propertiesFileLoader.getResourceBundleContainingProperties();
		String databaseType = properties.getString(PROP_NAME_FOR_DATABASE_TYPE).toUpperCase();
		
		if (databaseType.equals("POSTGRESQL"))
		{
			return DBType.POSTGRESQL;
		}
		else if (databaseType.equals("MYSQL"))
		{
			return DBType.MYSQL;
		}
		else
		{
			return null;
		}
	}
}
